package ui.frames;

import game.Game;
import game.components.Cordinent;
import game.components.Player;
import game.components.StartingPosition;
import game.components.squares.BasicSquare;
import game.components.squares.EmptySquare;
import game.components.squares.GamePiece;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.JFrame;

public class BoardFrame extends JFrame implements MouseListener
{

	private static final long	serialVersionUID	= 74504291346260235L;

	private BasicSquare[][]		board;
	private Container			contentPane;
	private Game				game;
	private boolean				currentSquareAlreadyHighlighted;

	public BoardFrame()
	{

		this.setSize(new Dimension(700, 700));
		this.setTitle("Stratigo Board");
		this.setResizable(false);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		contentPane = this.getContentPane();
		contentPane.setLayout(new GridLayout(10, 10));

		game = new Game(new Player("Player 1", StartingPosition.Top),
				new Player("Player 2", StartingPosition.Bottom));

		board = game.getBoard();

		game.addMouseListenerToPieces(this);

		redrawBoard();
	}

	public void clearHighlights()
	{
		for (int i = 0; i < board.length; i++)
		{
			for (int j = 0; j < board[i].length; j++)
			{
				if (!( board[i][j] instanceof GamePiece && ( (GamePiece) board[i][j] )
						.getSelected() ))
				{
					board[i][j].setHighlighted(false);
				}
			}
		}
	}

	public void highlightPossibleMoves(List<Cordinent> possibleMoves)
	{
		this.clearHighlights();
		for (Cordinent c : possibleMoves)
		{
			board[c.getX()][c.getY()].setHighlighted(true);
			board[c.getX()][c.getY()].repaint();
		}
	}

	@Override
	public void mouseClicked(MouseEvent e)
	{
		if (e.getComponent() instanceof GamePiece)
		{
			( (GamePiece) e.getComponent() ).setSelected(true);
			( (GamePiece) e.getComponent() ).repaint();
			this.currentSquareAlreadyHighlighted = true;
		}
		this.highlightPossibleMoves(this.game
				.getPossibleMoves(( (BasicSquare) e.getComponent() )));

		this.redrawBoard();
	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		BasicSquare s = (BasicSquare) e.getComponent();
		if (s.isHighlighted()
				|| ( ( e.getComponent() instanceof GamePiece ) && ( ( (GamePiece) e
						.getComponent() ).getSelected() ) ))
		{

			this.currentSquareAlreadyHighlighted = true;
		}
		else
		{
			this.currentSquareAlreadyHighlighted = false;
		}
		s.setHighlighted(true);
		s.repaint();
	}

	@Override
	public void mouseExited(MouseEvent e)
	{
		BasicSquare s = (BasicSquare) e.getComponent();
		if (!this.currentSquareAlreadyHighlighted)
		{
			s.setHighlighted(false);
		}
		s.repaint();
	}

	@Override
	public void mousePressed(MouseEvent e)
	{}

	@Override
	public void mouseReleased(MouseEvent e)
	{}

	public void movePiece(int initX, int initY, int endX, int endY)
	{
		board[endY][endX] = board[initY][initX];
		board[initY][initX] = new EmptySquare(this, new Cordinent(initX, initY));
		this.redrawBoard();
	}

	public void redrawBoard()
	{
		// There has to be a better way to do this...
		for (int i = 0; i < contentPane.getComponentCount(); i++)
		{
			contentPane.remove(i);
		}

		for (int i = 0; i < board.length; i++)
		{
			for (int j = 0; j < board.length; j++)
			{
				contentPane.add(board[j][i]);
			}
		}
		contentPane.validate();
		contentPane.repaint();
	}
}
